﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PersonalExpenseTracker.PersistenceLayer;

namespace PersonalExpenseTracker
{
    public partial class ManageCategory : Form
    {
        public int Id;
        public string CategoryName;
        public int ParentId;

        public ManageCategory(int parentCategoryId) : this(0, "", parentCategoryId)
        {
            this.Text = "Add New Category";
        }

        public ManageCategory(int categoryId, string categoryName, int parentCategoryId)
        {
            InitializeComponent();
            this.Text = "Edit Category";
            this.Id = categoryId;
            this.ParentId = parentCategoryId;
            this.CategoryName = categoryName;
            this.txtCategoryName.Text = this.CategoryName;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtCategoryName.Text.Trim().Length == 0)
            {
                MessageBox.Show("Please enter category name.", "Missing Information");
                return;
            }

            this.CategoryName = txtCategoryName.Text.Trim();

            if (this.Id == 0)
            {
                this.Id = 
                    PersistenceLayerFacade.AddCategory(
                        this.CategoryName, 
                        this.ParentId);
            }
            else
            {
                PersistenceLayerFacade.UpdateCategory(
                    this.Id, this.CategoryName, this.ParentId);
            }

            this.DialogResult = DialogResult.OK;
        }
    }
}
